home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1993…ch: Other People's Memory / ADC Developer CD (1993-03) (''Other People's Memory'')_iso / Dev.CD Mar 93.iso / Technical Documentation / Sample Code / Snippets / QuickTime / PlayMovie / PlayMovie.c next >
Encoding:
C/C++ Source or Header  |  1993-01-18  |  10.4 KB  |  444 lines  |  [TEXT/KAHL]

  1. /*--------------------------------------------------------------------------*/
  2. /*                                                                            */
  3. /*        PlayMovie.                                                            */
  4. /*            by John Wang                                                    */
  5. /*                                                                            */
  6. /*        Description:    This program demonstrates playing movies with        */
  7. /*            QuickTime.  It also allows the user to save the movies in a        */
  8. /*            flattened format using FlattenMovie().                            */
  9. /*                                                                            */
  10. /*        Version:        1.0 Completed 11/12/91                                */
  11. /*                                                                            */
  12. /*--------------------------------------------------------------------------*/
  13.  
  14. #include     <GestaltEqu.h>
  15. #include    "Movies.h"
  16.  
  17. #define Gestalttest        0xA1AD
  18. #define NoTrap            0xA89F
  19.  
  20. #define    appleID            128            
  21. #define    appleMenu        0
  22. #define    aboutMeCommand    1
  23.  
  24. #define    fileID            129
  25. #define openCommand        1
  26. #define    flattenCommand    2
  27. #define closeCommand    3
  28. #define    quitCommand     5
  29.  
  30. #define    aboutMeDLOG        128
  31. #define    okButton        1
  32.  
  33. #define    MAXWINDOWS        5
  34.  
  35. /*------------------------------------------------------*/
  36. /*    Global Variables.                                    */
  37. /*------------------------------------------------------*/
  38.  
  39. Boolean                DoneFlag = FALSE;
  40. MenuHandle            mymenu0, mymenu1;
  41. Boolean                playingMovie[MAXWINDOWS];
  42. Movie                myMovie[MAXWINDOWS];
  43. WindowPtr            myWindow[MAXWINDOWS];
  44. int                    startlocation;
  45.  
  46. /*------------------------------------------------------*/
  47. /*    getMovieFromFile().                                    */
  48. /*------------------------------------------------------*/
  49.  
  50. Movie getMovieFromFile()
  51. {
  52.     OSErr                    err;
  53.     StandardFileReply        reply;
  54.     Point                    where = {200, 50};
  55.     SFTypeList                types;
  56.     short                    movieResRefNum;
  57.     short                    actualResId;
  58.     Movie                    theMovie;
  59.     
  60.     types[0] = 'MooV';
  61.     StandardGetFilePreview(nil, 1, types, &reply);
  62.     if (!reply.sfGood) return((Movie) 0);
  63.  
  64.     err = OpenMovieFile(&reply.sfFile, &movieResRefNum, fsRdPerm);
  65.     if (GetMoviesError()) return((Movie) 0);
  66.     if (err) return ((Movie) 0);
  67.     actualResId = DoTheRightThing;
  68.  
  69.     err = NewMovieFromFile(&theMovie, movieResRefNum, &actualResId, (char *) 0, newMovieActive, (Boolean *) 0);
  70.     if (GetMoviesError()) return((Movie) 0);
  71.     if (err) return ((Movie) 0);
  72.  
  73.     err = CloseMovieFile(movieResRefNum);
  74.     if (GetMoviesError()) return((Movie) 0);
  75.     if (err) return ((Movie) 0);
  76.  
  77.     return (theMovie);
  78. }
  79.  
  80. /*------------------------------------------------------*/
  81. /*    playMovie().                                            */
  82. /*------------------------------------------------------*/
  83.  
  84. OSErr playMovie(int    index)
  85. {
  86.     Rect            movieBounds;
  87.     
  88.     GetMovieBox(myMovie[index], &movieBounds);
  89.     OffsetRect(&movieBounds, -movieBounds.left, -movieBounds.top);
  90.     if (movieBounds.right < 40) movieBounds.right = 40;
  91.     if (movieBounds.bottom < 20) movieBounds.bottom = 20;
  92.     SetMovieBox(myMovie[index], &movieBounds);
  93.     OffsetRect(&movieBounds, startlocation, startlocation);
  94.     myWindow[index] = NewCWindow(0L, &movieBounds, "\PMovie!", 1, 0, (WindowPtr) -1, TRUE, 0L);
  95.     startlocation += 50;
  96.     if (startlocation > 300) startlocation = 50;
  97.     
  98.     SetMovieGWorld(myMovie[index], (CGrafPtr) myWindow[index], 0);
  99.     if (GetMoviesError()) DebugStr("\PSetMovieGWorld error.");
  100.  
  101. /*    Uncomment these lines if you want to pre load the movie into ram.
  102.     GotoBeginningOfMovie(myMovie[index]);
  103.     if (LoadMovieIntoRam(myMovie[index], GetMovieTime(myMovie[index], 0L),
  104.                                     GetMovieDuration(myMovie[index]),
  105.                                     0) != noErr)
  106.         DebugStr("\PNot enough memory to load movie into ram.");
  107. */
  108.     SetMovieRate(myMovie[index], 0x00010000);
  109.  
  110. }
  111.  
  112. /*------------------------------------------------------*/
  113. /*    flatten().                                            */
  114. /*------------------------------------------------------*/
  115.  
  116. short flatten(int index)
  117. {
  118.     StandardFileReply        reply;
  119.     OSErr       theErr      = noErr;
  120.  
  121.     StandardPutFile("\PName of flattened movie.", "\PUntitled", &reply);
  122.     if (!reply.sfGood) return;
  123.  
  124.      if (theErr = GetMoviesError()) 
  125.         DebugStr("\PCall Before FlattenMovies failed.");
  126.  
  127.     FlattenMovie(myMovie[index],
  128.                 flattenAddMovieToDataFork,
  129.                 &reply.sfFile,
  130.                 'JWJW',
  131.                 0,
  132.                 createMovieFileDeleteCurFile,
  133.                 nil,
  134.                 nil);
  135.                 
  136.     if (theErr = GetMoviesError()) 
  137.         DebugStr("\PFlattenMovies failed.");
  138.   
  139.     return(theErr);
  140. }
  141.  
  142. /*------------------------------------------------------*/
  143. /*    showAboutMeDialog()                                    */
  144. /*------------------------------------------------------*/
  145.  
  146. void showAboutMeDialog()
  147. {
  148.     GrafPtr     savePort;
  149.     DialogPtr    theDialog;
  150.     short        itemHit;
  151.  
  152.     GetPort(&savePort);
  153.     theDialog = GetNewDialog(aboutMeDLOG, nil, (WindowPtr) -1);
  154.     SetPort(theDialog);
  155.  
  156.     do {
  157.         ModalDialog(nil, &itemHit);
  158.     } while (itemHit != okButton);
  159.  
  160.     CloseDialog(theDialog);
  161.  
  162.     SetPort(savePort);
  163.     return;
  164. }
  165.  
  166. /*------------------------------------------------------*/
  167. /*    init().                                                */
  168. /*------------------------------------------------------*/
  169.  
  170. void init()
  171. {
  172.     OSErr                err;
  173.     int                    i;
  174.     long                QDfeature, OSfeature;
  175.  
  176.     /*    Initialize Managaer.    */
  177.     InitGraf(&qd.thePort);
  178.     FlushEvents(everyEvent, 0);
  179.     InitWindows();
  180.     InitDialogs(nil);
  181.     InitCursor();
  182.  
  183.     /*    Set up menus.    */
  184.     mymenu0 = GetMenu(appleID);
  185.     AddResMenu(mymenu0, 'DRVR');
  186.     InsertMenu(mymenu0,0);
  187.     mymenu1 = GetMenu(fileID);
  188.     InsertMenu(mymenu1,0);
  189.     DrawMenuBar();
  190.     
  191.     /*    Set up variables.    */
  192.     startlocation = 50;
  193.     for (i = 0; i < MAXWINDOWS; i++) {
  194.         playingMovie[i] = FALSE;
  195.         myWindow[i] = nil;
  196.     }
  197.  
  198.     /*    Use Gestalt to find if QuickDraw and QuickTime is available.    */
  199.     if ((GetTrapAddress(Gestalttest) != GetTrapAddress(NoTrap))) {
  200.         err = Gestalt(gestaltQuickdrawVersion, &QDfeature);
  201.         if (err)
  202.             ExitToShell();
  203.         err = Gestalt(gestaltSystemVersion, &OSfeature);
  204.         if (err)
  205.             ExitToShell();
  206.         if (!DoneFlag && (QDfeature & 0x0f00) != 0x0200 && OSfeature < 0x0607)
  207.             ExitToShell();
  208.         err = Gestalt(gestaltQuickTime, &QDfeature);
  209.         if (err)
  210.             ExitToShell();
  211.     } else
  212.         ExitToShell();
  213.     
  214.     /*    Open QuickTime last.    */
  215.     err = EnterMovies();
  216.     if (err)
  217.         ExitToShell();
  218. }
  219.  
  220. /*------------------------------------------------------*/
  221. /*    finish().                                            */
  222. /*------------------------------------------------------*/
  223.  
  224. void finish()
  225. {
  226.     ExitMovies();
  227.     ExitToShell();
  228. }
  229.  
  230. /*------------------------------------------------------*/
  231. /*    doOpenCommand().                                        */
  232. /*------------------------------------------------------*/
  233.  
  234. void doOpenCommand()
  235. {
  236.     int            i, useThisIndex;
  237.     
  238.     useThisIndex = -1;
  239.     
  240.     /*    Search for the first window that is nil.    */
  241.     for (i = MAXWINDOWS-1; i >= 0; i--)
  242.         if (myWindow[i] == nil)
  243.             useThisIndex = i;
  244.             
  245.     /*    If index = -1, then it means that there are no windows avaiable.    */
  246.     if (useThisIndex != -1) {
  247.         myMovie[useThisIndex] = getMovieFromFile();
  248.         if (myMovie[useThisIndex] != 0) {
  249.             playMovie(useThisIndex);
  250.             playingMovie[useThisIndex] = TRUE;
  251.         }
  252.     }
  253. }
  254.  
  255. /*------------------------------------------------------*/
  256. /*    doFlattenCommand().                                        */
  257. /*------------------------------------------------------*/
  258.  
  259. void doFlattenCommand()
  260. {
  261.     int            i, useThisIndex;
  262.     WindowPtr    myTempWindow;
  263.     
  264.     /*    Flatten movie that is currently selected.    */
  265.     myTempWindow = FrontWindow();
  266.     if (myTempWindow == nil) return;
  267.     for (i = 0; i < MAXWINDOWS; i++)
  268.         if (myWindow[i] == myTempWindow)
  269.             flatten(i);
  270. }
  271.  
  272. /*------------------------------------------------------*/
  273. /*    doCloseCommand().                                        */
  274. /*------------------------------------------------------*/
  275.  
  276. void doCloseCommand()
  277. {
  278.     int        i, useThisIndex;
  279.     WindowPtr    myTempWindow;
  280.  
  281.     /*    Close selected window.    */
  282.     myTempWindow = FrontWindow();
  283.     if (myTempWindow == nil) return;
  284.     for (i = 0; i < MAXWINDOWS; i++)
  285.         if (myWindow[i] == myTempWindow) {
  286.             DisposeMovie(myMovie[i]);
  287.             DisposeWindow(myTempWindow);
  288.             playingMovie[i] = FALSE;
  289.             myWindow[i] = nil;
  290.         }
  291. }
  292.  
  293. /*------------------------------------------------------*/
  294. /*    doCommand().                                        */
  295. /*------------------------------------------------------*/
  296.  
  297. void doCommand(mResult)
  298.     long    mResult;
  299. {
  300.     int                     theMenu, theItem;
  301.     char                    daName[256];
  302.     GrafPtr                 savePort;
  303.  
  304.     theItem = LoWord(mResult);
  305.     theMenu = HiWord(mResult);
  306.     
  307.     switch (theMenu) {
  308.         case appleID:
  309.             if (theItem == aboutMeCommand)
  310.                 showAboutMeDialog();
  311.             else {
  312.                 GetItem(mymenu0, theItem, daName);
  313.                 GetPort(&savePort);
  314.                 (void) OpenDeskAcc(daName);
  315.                 SetPort(savePort);
  316.             }
  317.             break;
  318.  
  319.         case fileID:
  320.             switch (theItem) {
  321.                 case openCommand:
  322.                     doOpenCommand();
  323.                     break;
  324.                 case flattenCommand:
  325.                     doFlattenCommand();
  326.                     break;
  327.                 case closeCommand:
  328.                     doCloseCommand();
  329.                     break;
  330.                 case quitCommand:
  331.                     DoneFlag = TRUE;
  332.                     break;
  333.                 default:
  334.                     break;
  335.                 }
  336.             break;
  337.     }
  338.     HiliteMenu(0);
  339.     return;
  340. }
  341.  
  342. /*------------------------------------------------------*/
  343. /*    playMovies().                                            */
  344. /*------------------------------------------------------*/
  345.  
  346. playMovies()
  347. {
  348.     int        i;
  349.     
  350.     for (i = 0; i < MAXWINDOWS; i++)
  351.         if (playingMovie[i] == TRUE) {
  352.             if (IsMovieDone(myMovie[i]) == FALSE)
  353.                 MoviesTask(myMovie[i], DoTheRightThing);
  354.             else {
  355.                 DisposeMovie(myMovie[i]);
  356.                 DisposeWindow((WindowPtr) myWindow[i]);
  357.                 playingMovie[i] = FALSE;
  358.                 myWindow[i] = nil;
  359.             }
  360.         }
  361. }
  362.  
  363. /*------------------------------------------------------*/
  364. /*    main().                                                */
  365. /*------------------------------------------------------*/
  366.  
  367. main()
  368. {
  369.     int                    i;
  370.     char            key;
  371.     Boolean            track;
  372.     long            growResult;
  373.     EventRecord     myEvent;
  374.     WindowPtr        whichWindow;
  375.     int                yieldTime;
  376.  
  377.  
  378.     init();
  379.     yieldTime = 0;
  380.     for ( ;; ) {
  381.     
  382.         /*    We can't just do ExitToShell because we must cann ExitMovies.    */
  383.         if (DoneFlag)
  384.             finish();
  385.             
  386.         /*    Play movies which are active.    */
  387.         playMovies();
  388.  
  389.         if (WaitNextEvent(everyEvent, &myEvent, yieldTime, nil)) {
  390.             switch (myEvent.what) {
  391.                 case mouseDown:
  392.                     switch (FindWindow(myEvent.where, &whichWindow)) {
  393.                         case inSysWindow:
  394.                             SystemClick(&myEvent, whichWindow);
  395.                             break;
  396.                         case inMenuBar:
  397.                             doCommand(MenuSelect(myEvent.where));
  398.                             break;
  399.                         case inContent:
  400.                             SelectWindow(whichWindow);
  401.                             break;
  402.                         case inDrag:
  403.                             DragWindow (whichWindow, myEvent.where, &qd.screenBits.bounds);
  404.                             break;
  405.                         case inGrow:
  406.                             break;
  407.                         case inGoAway:
  408.                             track = TrackGoAway (whichWindow, myEvent.where);
  409.                             if (track)    doCloseCommand();
  410.                             break;
  411.                         case inZoomIn:
  412.                             break;
  413.                         case inZoomOut:
  414.                             break;
  415.                         default:
  416.                             break;
  417.                     }
  418.                     break;
  419.                 case keyDown:
  420.                 case autoKey:
  421.                     key = myEvent.message & charCodeMask;
  422.                     if ( myEvent.modifiers & cmdKey )
  423.                         if ( myEvent.what == keyDown )
  424.                             doCommand(MenuKey(key));
  425.                     break;
  426.                 case updateEvt:
  427.                     for (i = 0; i < MAXWINDOWS; i++)
  428.                         if ((WindowPtr) myEvent.message == myWindow[i]) {
  429.                             BeginUpdate((WindowPtr) myWindow[i]);
  430.                             EndUpdate((WindowPtr) myWindow[i]);
  431.                         }
  432.                     break;
  433.                 case diskEvt:
  434.                     break;
  435.                 case activateEvt:
  436.                     break;
  437.                 case app4Evt:
  438.                     break; 
  439.                 default:
  440.                     break;
  441.             }
  442.         }
  443.     }
  444. }